<?php

class Core_Agent_Component_Account_Controller_Account extends Dcmp_Component_Controller {

    public function registerAction(){
        Kd_Web::getPage()->setTitle('会员注册');
        Kd_Web::getPage()->getNavigation()->addLabel('会员注册');
        return $this->templateResult();
    }

    public function registerEmptyAction(){
        return $this->templateResult();
    }

    public function registerPostAction(){
        $validateCode = $this->getRequest()->getForm('validatecode');
        if(strcasecmp($validateCode,Kd_Web::getSession()->get('validate_code')) !=0){
            $this->getMessager()->addError($this->__('agent.label.error_validateimg'));
            $this->viewResult(NULL,'register');
        }
        $username = $this->getRequest()->getForm('username');
        $password = $this->getRequest()->getForm('password');
        $model = App::getModel('permission/account');
            $model->setUserName($username)
                  ->setPassword(md5($password))
                  ->setFullName($username)
                  ->setGrade('na');
            try{
                $model->validate();
                $model->save();
                $this->getMessager()->addInform($this->__('frontend.message.registered_success'));
                $identity = Kd_Web::getIdentity();
                $identity = $identity->setId($model->getId())
                                 ->setName($model->getUserName())
                                 ->setGrade($model->getGrade())
                                 ->login();
                if(eregi("/agent/account/account/register", $_SERVER['HTTP_REFERER'])){
                    return $this->redirectResult($this->getUrl(NULL,'index','index','index'));
                }
                return $this->redirectResult($_SERVER['HTTP_REFERER']);
            }catch(Core_Model_Permission_Exception_Account $ex){
                switch($ex->getStatusCode()){
                    case Core_Model_Permission_Exception_Account::ACCOUNT_REPEATED:
                        $this->getMessager()->addError($this->__f('frontend.message.account_repeated', $username));
                }
            }
            return $this->redirectResult($this->getUrl(NULL,'register','account'));
    }

    public function loginAction(){
        Kd_Web::getPage()->setTitle('会员登陆');
        Kd_Web::getPage()->getNavigation()->addLabel('会员登陆');
        return $this->templateResult();
    }

    public function loginEmptyAction(){
        return $this->templateResult();
    }

    public function loginPostAction(){
            $validateCode = $this->getRequest()->getForm('validatecode');
            if(strcasecmp($validateCode,Kd_Web::getSession()->get('validate_code'))!=0){
                $this->getMessager()->addError($this->__('agent.label.error_validateimg'));
                return $this->redirectResult($this->getUrl(NULL,'login'));
            }
            $username = $this->getRequest()->getForm('username');
            $password = $this->getRequest()->getForm('password');
            $model = App::getModel('permission/account')->loadByUserName($username);
            if(!$model->getId()){
                $this->getMessager()->addError($this->__('agent.label.undefine_username'));
                return $this->redirectResult($this->getUrl(NULL,'login'));
            }
            if($model->getPassword() != md5($password)){
                    $this->getMessager()->addError($this->__('agent.label.error_password'));
                    return $this->redirectResult($this->getUrl(NULL,'login'));
            }
            $rand = rand();
            $identity = Kd_Web::getIdentity();
            $identity = $identity->setId($model->getId())
                                 ->setName($model->getUserName())
                                 ->setGrade($model->getGrade())
                                 ->login();
			return $this->redirectResult($this->getUrl(NULL,'index','index','index'));
    }
    
    public function logoutAction(){
        Kd_Web::getIdentity()->logout();
        Kd_Web::getSession()->abort();
        return $this->redirectResult($this->getUrl(NULL,'login'));
    }

}